export const lookMixin = {
  data () {
    return {

    }
  },
  methods: {
    showRecommend () {
      this.$refs.recommendLookDraw.show()
    }
  }
}
import { querySkus, queryStock } from '@/api/box/workbench'
import Address from './module/address'
import AddressEdit from '@/views/box/components/Address/AddressEdit.vue'
import GoodInfoModal from '../../components/GoodInfoModal'
export const colMixin = {
  data () {
    return {

    }
  },
  components: { Address, AddressEdit, GoodInfoModal },
  methods: {
    // 查询商品库存
    fetchStock (skus) {
      const data = {
        requestData: {
          skuIds: skus,
          unionId: this.unionId
        }
      }
      return queryStock(data)
        .then(res => {
          if (res.code === 0) {
            return res.data
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(err => {
          console.log('fetchStock:' + err)
        })
    },
    // 查询商品所有SKU
    fetchQuerySkus (record) {
      if (record.skus.length === 0) {
        const data = {
          requestData: {
            productId: record.productId
          }
        }
        return querySkus(data)
          .then(res => {
            if (res.code === 0) {
              if (res.data.length > 0) {
                record.skus = res.data
                const colors = this.filterSizeColor(res.data, record.colorList, 'color')
                record.colorList = colors
                const sizes = this.filterSizeColor(res.data, record.sizeList, 'size')
                record.sizeList = sizes
              }
            }
          })
          .catch(err => {
            console.log('查询商品所有SKU:' + err)
          })
      }
    },
    // 筛选颜色尺码组
    filterSizeColor (data, list, type) {
      switch (type) {
        case 'color':
          const tempArr = list.map(item => item.colorno)
          const colors = data.map(item => {
            const color = {
              color_name: item.color_name,
              colorno: item.colorno
            }
            return color
          })
          for (var i = 0; i < colors.length; i++) {
            var temp = colors[i]
            if (tempArr.indexOf(temp.colorno) === -1) {
              tempArr.push(temp.colorno)
              list.push(temp)
            }
          }
          return list
        case 'size':
          const tempArr2 = list.map(item => item.sizeno)
          const sizes = data.map(item => {
            const size = {
              size_name: item.size_name,
              sizeno: item.sizeno
            }
            return size
          })
          for (var j = 0; j < sizes.length; j++) {
            var temp2 = sizes[j]
            if (tempArr2.indexOf(temp2.sizeno) === -1) {
              tempArr2.push(temp2.sizeno)
              list.push(temp2)
            }
          }
          list.sort(this.compare('sizeno'))
          return list
      }
    },
    // 尺码排序
    compare (property) {
      return function (a, b) {
        var value1 = a[property]
        var value2 = b[property]
        return value1 - value2
      }
    },
    // 查询对应sku
    async checkSku (colorsize, record, shoppingList) {
      try {
        // 查询对应sku
        const result = record.skus.filter(item => item.colorno == colorsize[0] && item.sizeno == colorsize[1])
        const stock = await this.fetchStock([result[0].id])
        // 检查商品是否有重复
        const goods = shoppingList.filter(item => item.skuId.toString() === result[0].id)
        if (goods.length === 0) {
          if (stock[0].stock > 0) {
            this.updateStylingCart(result[0], record, stock[0].stock, stock[0].ifEb)
          } else {
            this.$message.warning('该尺码库存为0！')
          }
        } else {
          this.$message.warning('已存在商品！')
        }
      } catch (err) {
        console.log(err)
      }
    },
    async handleSkuOptions (item) {
      let skuOptions = []
      if (item.skus.length == 0) {
        await this.fetchQuerySkus(item)
      }
      if (item.skus.length == 0) {
        item.disabled = true
        return
      }
      // 处理库存
      let skuIds = item.skus.map(i => i.id)
      let itemStock = await this.fetchStock(skuIds)
      for (let sku of item.skus) {
        let result = itemStock.filter(e => e.skuId == sku.id)
        if (result.length > 0) {
          sku.stock = result[0].stock > 0 ? result[0].stock : 0
          sku.ifEb = result[0].ifEb
        } else {
          sku.stock = 0
          sku.ifEb = false
        }
      }
      // 尺码
      let sizeOption = []
      for (let size of item.sizeList) {
        let children = {
          label: size.size_name,
          value: size.sizeno
        }
        sizeOption.push(children)
      }

      for (let option of item.colorList) {
        let obj = { value: option.colorno, label: option.color_name, children: JSON.parse(JSON.stringify(sizeOption)), disabled: false }
        for (let size of obj.children) {
          let result = item.skus.filter(e => e.colorno == option.colorno && e.sizeno == size.value)
          if (result.length > 0) {
            size.skuId = result[0].id
            size.stock = result[0].stock
            size.disabled = result[0].stock == 0 ? true : false
            size.imgUrl = result[0].imgurl
          }
        }
        skuOptions.push(obj)
      }
      item.skuOptions = skuOptions

      item.currentSku = {
        stock: item.stock,
        colorNo: item.colorNo,
        sizeNo: item.sizeNo,
        id: item.skuId,
        imgUrl: item.imgUrl,
        ifEb: item.ifEb
      }
    }
  }
}